package com.gqj.meter.service.impl;

import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdWhAreaInfo;
import com.gqj.meter.mapper.MdWhAreaInfoMapper;
import com.gqj.meter.service.IMdWhAreaInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * 库区信息Service业务层处理
 *
 * @author lxq
 * @date lxq-01-09
 */
@Service
public class MdWhAreaInfoServiceImpl implements IMdWhAreaInfoService
{
    @Autowired
    private MdWhAreaInfoMapper mdWhAreaInfoMapper;

    @Autowired
    protected Validator validator;
    private static final Logger log = LoggerFactory.getLogger(MdWhInfoServiceImpl.class);
    /**
     * 查询库区信息
     *
     * @param whAreaId 库区信息主键
     * @return 库区信息
     */
    @Override
    public MdWhAreaInfo selectMdWhAreaInfoByWhAreaId(Long whAreaId)
    {
        return mdWhAreaInfoMapper.selectMdWhAreaInfoByWhAreaId(whAreaId);
    }

    /**
     * 查询库区信息列表
     *
     * @param mdWhAreaInfo 库区信息
     * @return 库区信息
     */
    @Override
    public List<MdWhAreaInfo> selectMdWhAreaInfoList(MdWhAreaInfo mdWhAreaInfo)
    {
        return mdWhAreaInfoMapper.selectMdWhAreaInfoList(mdWhAreaInfo);
    }

    /**
     * 新增库区信息
     *
     * @param mdWhAreaInfo 库区信息
     * @return 结果
     */
    @Override
    public int insertMdWhAreaInfo(MdWhAreaInfo mdWhAreaInfo)
    {
        return mdWhAreaInfoMapper.insertMdWhAreaInfo(mdWhAreaInfo);
    }

    /**
     * 修改库区信息
     *
     * @param mdWhAreaInfo 库区信息
     * @return 结果
     */
    @Override
    public int updateMdWhAreaInfo(MdWhAreaInfo mdWhAreaInfo)
    {
        return mdWhAreaInfoMapper.updateMdWhAreaInfo(mdWhAreaInfo);
    }

    /**
     * 批量删除库区信息
     *
     * @param whAreaIds 需要删除的库区信息主键
     * @return 结果
     */
    @Override
    public int deleteMdWhAreaInfoByWhAreaIds(Long[] whAreaIds)
    {
        return mdWhAreaInfoMapper.deleteMdWhAreaInfoByWhAreaIds(whAreaIds);
    }

    /**
     * 删除库区信息信息
     *
     * @param whAreaId 库区信息主键
     * @return 结果
     */
    @Override
    public int deleteMdWhAreaInfoByWhAreaId(Long whAreaId)
    {
        return mdWhAreaInfoMapper.deleteMdWhAreaInfoByWhAreaId(whAreaId);
    }

    /**
     * 校验库区编号是否唯一
     *
     * @param mdWhAreaInfo 库区信息
     * @return 结果
     */
    @Override
    public String checkWhAreaNoUnique(MdWhAreaInfo mdWhAreaInfo) {
        Long whAreaId = StringUtils.isNull(mdWhAreaInfo.getWhAreaId()) ? -1L : mdWhAreaInfo.getWhAreaId();
        MdWhAreaInfo info = mdWhAreaInfoMapper.checkWhAreaNoUnique(mdWhAreaInfo.getWhAreaNo());
        if (StringUtils.isNotNull(info) && info.getWhAreaId().longValue() != whAreaId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询库区下拉框
     *
     * @param mdWhAreaInfo 库区
     * @return 库区集合
     */
    @Override
    public List<Map<String, Object>> mdWhAreaInfoOptions(MdWhAreaInfo mdWhAreaInfo){
        return mdWhAreaInfoMapper.mdWhAreaInfoOptions(mdWhAreaInfo);
    }

    /**
     * 导入库区数据
     *
     * @param mdWhAreaInfoList 库区列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importMdWhAreaInfo(List<MdWhAreaInfo> mdWhAreaInfoList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(mdWhAreaInfoList) || mdWhAreaInfoList.size() == 0) {
            throw new ServiceException("导入库区信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdWhAreaInfo whAreaInfo : mdWhAreaInfoList) {
            try {
                BeanValidators.validateWithException(validator, whAreaInfo);
                // 验证是否存在这个数据whNo
                MdWhAreaInfo w = mdWhAreaInfoMapper.selectMdWhAreaInfoByWhAreaNo(whAreaInfo.getWhAreaNo());
                if (StringUtils.isNull(w)) {
                    //查询当前最大id
                    Long whAreaId = mdWhAreaInfoMapper.selectMaxId();
                    if (null == whAreaId) {
                        whAreaId = (long) 1;
                    } else {
                        whAreaId = whAreaId + 1;
                    }
                    whAreaInfo.setWhAreaId(whAreaId);
                    this.insertMdWhAreaInfo(whAreaInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、库区编号 " + whAreaInfo.getWhAreaNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    whAreaInfo.setWhAreaId(w.getWhAreaId());
                    this.updateMdWhAreaInfo(whAreaInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、库区编号 " + whAreaInfo.getWhAreaNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、库区编号 " + whAreaInfo.getWhAreaNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、库区编号 " + whAreaInfo.getWhAreaNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
